// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-15 16:59:17
// 生成路径: internal/app/system/logic/withdraw_records.go
// 生成人：周
// desc:余额提现记录
// company:云南奇讯科技有限公司
// ==========================================================================

package withdrawRecords

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterWithdrawRecords(New())
}

func New() service.IWithdrawRecords {
	return &sWithdrawRecords{}
}

type sWithdrawRecords struct{}

func (s *sWithdrawRecords) List(ctx context.Context, req *model.WithdrawRecordsSearchReq) (listRes *model.WithdrawRecordsSearchRes, err error) {
	listRes = new(model.WithdrawRecordsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.WithdrawRecords.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.WithdrawRecords.Columns().Id+" = ?", req.Id)
		}
		if req.Userid != "" {
			m = m.Where(dao.WithdrawRecords.Columns().Userid+" = ?", gconv.Int(req.Userid))
		}
		if req.Status != "" {
			m = m.Where(dao.WithdrawRecords.Columns().Status+" = ?", gconv.Int(req.Status))
		}
		if req.Reason != "" {
			m = m.Where(dao.WithdrawRecords.Columns().Reason+" = ?", req.Reason)
		}
		if req.SubmitTime != "" {
			m = m.Where(dao.WithdrawRecords.Columns().SubmitTime+" = ?", gconv.Time(req.SubmitTime))
		}
		if req.ArriveTime != "" {
			m = m.Where(dao.WithdrawRecords.Columns().ArriveTime+" = ?", gconv.Time(req.ArriveTime))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.WithdrawRecords.Columns().CreatedAt+" >=? AND "+dao.WithdrawRecords.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.WithdrawRecordsListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.WithdrawRecordsListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.WithdrawRecordsListRes{
				Id:         v.Id,
				Userid:     v.Userid,
				Money:      v.Money,
				Balance:    v.Balance,
				Status:     v.Status,
				Reason:     v.Reason,
				SubmitTime: v.SubmitTime,
				ArriveTime: v.ArriveTime,
				CreatedAt:  v.CreatedAt,
			}
		}
	})
	return
}

func (s *sWithdrawRecords) GetById(ctx context.Context, id int) (res *model.WithdrawRecordsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.WithdrawRecords.Ctx(ctx).WithAll().Where(dao.WithdrawRecords.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sWithdrawRecords) Add(ctx context.Context, req *model.WithdrawRecordsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.WithdrawRecords.Ctx(ctx).Insert(do.WithdrawRecords{
			Status: req.Status,
			Reason: req.Reason,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sWithdrawRecords) Edit(ctx context.Context, req *model.WithdrawRecordsEditReq) (err error) {

	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.WithdrawRecords.Ctx(ctx).WherePri(req.Id).Update(do.WithdrawRecords{
			Status: req.Status,
			Reason: req.Reason,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sWithdrawRecords) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.WithdrawRecords.Ctx(ctx).Delete(dao.WithdrawRecords.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

// 余额提现记录提现状态：1-待审核  2-已到账 3-审核失败  修改（状态）
func (s *sWithdrawRecords) ChangeStatus(ctx context.Context, id int, status int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.WithdrawRecords.Ctx(ctx).WherePri(id).
			Update(do.WithdrawRecords{
				Status: status,
			})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}
